package com.example.wcdb;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.wcdb.config.DbHelper;
import com.example.wcdb.config.database.WCDBDatabase;

/**
 * 针对WCDB数据库框架编写的数据库迁移和升级类，添加了master保存的逻辑
 * @author zhangxin221
 */
public abstract class BaseMigrate extends Migration {
    private static final String TAG = BaseMigrate.class.getName();
    public BaseMigrate(int startVersion, int endVersion) {
        super(startVersion, endVersion);
    }

    public void migrate(@NonNull SupportSQLiteDatabase db) {
        DbHelper.checkIsInit();
        wcdbMigrate(db);
        //添加master表信息的备份
        RepairableDatabase.getDatabase().saveMaster(new RepairableDatabase.OperateCallback() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "数据库升级备份成功：" + startVersion + "-" + endVersion);
            }

            @Override
            public void onError(String errMsg) {
                Log.d(TAG, "数据库升级备份失败：" + startVersion + "-" + endVersion + "，" + errMsg);
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "数据库升级备份取消：" + startVersion + "-" + endVersion);
            }
        });
    }

    public abstract void wcdbMigrate(@NonNull SupportSQLiteDatabase database);
}
